<?php
namespace app\index\controller\user;

use app\common\model\admin\AccountModel;
use app\common\model\index\UserModel;
use app\index\controller\BaseIndex;
use think\facade\Validate;

/**
 * 个人信息
 * @package app\index\controller\user
 * @author wangqinghui
 * @DateTime 2024-09-12 11:17:07
 */
class Profile extends BaseIndex
{

    // 是否强制登录
    protected $need_login = true;

    /**
     * 个人信息页面
     * @author wangqinghui
     * @DateTime 2024-09-12 12:00:59
     * @return void
     */
    public function index()
    {
        $this->title = lang('我的个人信息');
        $userInfo = $this->userInfo;
        $userInfo['sex'] = UserModel::getSexArr()[$userInfo['sex']] ?? '未知';
        $userInfo['edu_background'] = UserModel::getBackgroundArr()[$userInfo['edu_background']] ?? '未知';
        $job_place_name = AccountModel::mk()->where('id', $userInfo['job_place'])->value('title');
        $userInfo['job_place'] = $job_place_name ?? '未知';
        $this->fetch('index', ['user' => $userInfo]);
    }

    /**
     * 编辑个人信息
     * @author wangqinghui
     * @DateTime 2024-09-12 12:01:34
     * @return void
     */
    public function edit()
    {
        UserModel::mForm('form', 'id', [], ['id' => $this->userInfo['id']]);
    }

    /**
     * 处理数据
     * @author wangqinghui
     * @DateTime 2024-09-13 11:19:41
     * @param [type] $data
     * @return void
     */
    protected function _edit_form_filter(&$data)
    {
        if($this->request->isPost()){
            $rule = [
                'id|用户ID' => 'require|number',
                'true_name|姓名'    => 'require|chsAlpha|length:1,50',
                'avatar|头像'      => 'require|length:1,255',
                'mobile|手机号'    => 'require|mobile',
                'email|邮箱'       => 'email',
                'nick_name|昵称'    =>  'require|length:1,50',
                'job_place|学校/单位'   =>  'require|integer|gt:0',
                'job_depart|院系/部门'  =>   'chsAlphaNum|length:1,100',
                'major|专业'        =>  'chsAlphaNum|length:1,100',
                'job_number|学号/工号'  =>  'require|alphaNum|length:1,50',
                'sex|性别'          =>  'require|number|in:0,1,2',
                'edu_background|最高学历'   =>  'require|number|in:0,1,2,3,4,5,6,7,8',
                'intro|简介'        =>  'length:1,500',
            ];
            $validate = Validate::rule($rule);
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
            // 校验姓名|昵称|手机号|邮箱是否唯一
            if(!empty($data['true_name']) || !empty($data['nick_name']) || !empty($data['mobile']) || !empty($data['email'])){
                $map = [
                    ['is_deleted', '=', 0],
                    ['id', '<>', $data['id']]
                ];
                $map_or = [];
                if(!empty($data['true_name'])){
                    $map_or[] = ['true_name', '=', $data['true_name']];
                }
                if(!empty($data['nick_name'])){
                    $map_or[] = ['nick_name', '=', $data['nick_name']];
                }
                if(!empty($data['mobile'])){
                    $map_or[] = ['mobile', '=', $data['mobile']];
                }
                if(!empty($data['email'])){
                    $map_or[] = ['email', '=', $data['email']];
                }
                
                $user_info = UserModel::mk()->where($map)->where(function ($query) use ($map_or) {
                    $query->whereOr($map_or);
                })->field('id,true_name,nick_name,mobile,email')->find();
                if(!empty($user_info)){
                    $err_msg = '信息被占用';
                    if(!empty($data['true_name']) && ($user_info['true_name'] == $data['true_name'])){
                        $err_msg = '姓名已被占用';
                    }elseif(!empty($data['nick_name']) && ($user_info['nick_name'] == $data['nick_name'])){
                        $err_msg = '昵称已被占用';
                    }elseif(!empty($data['mobile']) && ($user_info['mobile'] == $data['mobile'])){
                        $err_msg = '手机号已被占用';
                    }elseif(!empty($data['email']) && ($user_info['email'] == $data['email'])){
                        $err_msg = '邮箱已被占用';
                    }
                    $this->error($err_msg);
                }
            }
            unset($data['file']);
        }else{
            $this->sexArr = UserModel::getSexArr();
            $this->backGroundArr = UserModel::getBackgroundArr();
            // 获取所属平台
            $map = [
                ['status',  '=', 1],
                ['module',  'find in set', AccountModel::MODULE_SCHOOLS]
            ];
            $this->platforms = AccountModel::mk()->field('id,title')->where($map)->select()->toArray();
        }
    }
}